package com.bw.zyk.zhanggm.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.zyk.zhanggm.product.entity.Category;
import com.bw.zyk.zhanggm.product.service.ICategoryService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-04-23
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    ICategoryService iCategoryService;

    /**
     * 根据parentId查询子分类
     * @param parentId
     * @return
     */
    @GetMapping("getListByPid")
    public AjaxResult getListByPid(@RequestParam(value = "parentId",defaultValue = "0") Integer parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Category::getParentId, parentId);
        List<Category> list = iCategoryService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("getAll")
    public AjaxResult getAll() {
        List<Category> list = iCategoryService.list();
        return AjaxResult.success(list);
    }

    @GetMapping("getTreeList")
    public AjaxResult getTreeList() {
        //查询所有数据，空间换时间
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> list = iCategoryService.list(queryWrapper);
        //封装成树结构
        List<Category> cate1List = list.stream().filter(item -> item.getParentId().equals(0L)).collect(Collectors.toList());
        cate1List.forEach(cate1->{
            List<Category> cate2List = list.stream().filter(item -> item.getParentId().equals(cate1.getId())).collect(Collectors.toList());
            if (cate2List != null && cate2List.size() > 0) {
                cate1.setCategoryList(cate2List);
            }
            cate2List.forEach(cate2->{
                List<Category> cate3List = list.stream().filter(item -> item.getParentId().equals(cate2.getId())).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(cate3List)) {
                    cate2.setCategoryList(cate3List);
                }
            });
        });
        return AjaxResult.success(cate1List);

    }


    @GetMapping("getTreeList1")
    public AjaxResult getTreeList1() {
        //查询所有数据，空间换时间
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> list = iCategoryService.list(queryWrapper);
        //封装成树结构
        List<Category> cate1List = list.stream().filter(item -> item.getParentId().equals(0L)).collect(Collectors.toList());
        tree(list, cate1List);
        return AjaxResult.success(cate1List);
    }


    public void tree(List<Category> alllist,List<Category> cate1List) {
        log.info("cate1List:{}",cate1List);
        cate1List.forEach(cate->{
            List<Category> chiren = alllist.stream().filter(item -> item.getParentId().equals(cate.getId())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(chiren)) {
                cate.setCategoryList(chiren);
                tree(alllist, chiren);
            }
        });
    }

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now);
    }

}
